#!/usr/bin/env bash

#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
# 
#   http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#

set -eu

sasl_config_dir=$BUILD_DIR/not_there_at_all

WORK_DIR=$WORK_DIR/sasl_no_dir
mkdir $WORK_DIR

LOG_FILE=$WORK_DIR/qpidd.log

echo "Starting broker"
qpidd                                          \
    -p 0  --interface 127.0.0.1                \
    --no-data-dir                              \
    --auth=yes                                 \
    --mgmt-enable=yes                          \
    --log-enable info+                         \
    --log-source yes                           \
    --log-to-file ${LOG_FILE}                  \
    --sasl-config=$sasl_config_dir             \
    -d 2> /dev/null 1> $WORK_DIR/broker_port || :

# If it works right, the output will look something like this:  ( two lines long )
# Daemon startup failed: SASL: sasl_set_path failed: no such directory: /home/mick/trunk/qpid/cpp/src/tests/sasl_config (qpid/broker/SaslAuthenticator.cpp:112)
# 2011-10-13 14:07:00 critical qpidd.cpp:83: Unexpected error: Daemon startup failed: SASL: sasl_set_path failed: no such directory: /home/mick/trunk/qpid/cpp/src/tests/sasl_config (qpid/broker/SaslAuthenticator.cpp:112)

result=$(cat ${LOG_FILE} | grep "sasl_set_path failed: no such directory" | wc -l)

if (( $result == 2 )); then
    echo "Result: success"
    exit 0
fi

broker_port=$(cat $WORK_DIR/broker_port)
qpidd --port ${broker_port} --quit

echo "Result: fail"
exit 1
